package managedbean.FRM.SupplierPayment;

import entity.SCM.Supplier;
import entity.FRM.SupplierInvoice;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.FRM.SupplierPaymentBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="addSupplierPaymentManagerBean")
@ViewScoped
public class AddSupplierPaymentsManagerBean implements Serializable {
    public AddSupplierPaymentsManagerBean() {}
    
    @EJB
    private SupplierPaymentBeanLocal supplierPaymentBeanLocal;
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    
    private Supplier selectedSupplier;
    private SupplierInvoice selectedSupplierInvoice;
    private Double paymentAmount;
    private boolean isExisting;
    private boolean isNotExisting;
    private String supplierName;
    
    private boolean isSelectedInvoice;
    private String statusMessage;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedSupplier!=null)
            {
                this.setIsExisting(true);
                this.setIsNotExisting(false);
            }
            else
            {
                this.setIsExisting(false);
                this.setIsNotExisting(true);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public Double getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(Double paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public boolean isIsSelectedInvoice() {
        return isSelectedInvoice;
    }

    public void setIsSelectedInvoice(boolean isSelectedInvoice) {
        this.isSelectedInvoice = isSelectedInvoice;
    }

    
    public boolean isIsNotExisting() {
        return isNotExisting;
    }

    public void setIsNotExisting(boolean isNotExisting) {
        this.isNotExisting = isNotExisting;
    }
    
    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {        
            this.isExisting= isExisting;                
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    
    public SupplierInvoice getSelectedSupplierInvoice() {
        return selectedSupplierInvoice;
    }

    public void setSelectedSupplierInvoice(SupplierInvoice selectedSupplierInvoice) {
        this.selectedSupplierInvoice = selectedSupplierInvoice;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Supplier getSelectedSupplier() {
        return selectedSupplier;
    }

    public void setSelectedSupplier(Supplier selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }
    
    public void retrieve()
    {
        if (supplierName!=null)
        {
            this.setSelectedSupplier(supplierBeanLocal.findSupplierByName(supplierName));
        }
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Supplier> list = supplierBeanLocal.getAllSupplier();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSupplierName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSupplierName());
        }
        return results;  
    }
    
    public void makePayment(ActionEvent event) throws IOException
    {
        selectedSupplierInvoice = this.getSelectedSupplierInvoice();
        if (selectedSupplierInvoice !=null)
        {
            this.setIsSelectedInvoice(true);
        }        
    }
    
    public void confirmPayment(ActionEvent event) throws IOException
    {
        if (selectedSupplierInvoice !=null)
        {
            Double temp_InvoiceAmount = this.getSelectedSupplierInvoice().getSupplierInvoiceAmount();
            if (!paymentAmount.equals(temp_InvoiceAmount))
            {
                statusMessage="Payment failed: Amount is not correct";
            }
            else
            {
                supplierPaymentBeanLocal.createSupplierPayment(selectedSupplierInvoice.getSupplierInvoiceId(), paymentAmount);
                statusMessage = "New payment Saved Successfully";
            }
        }
        else
            statusMessage= "Invalid: SupplierInvoice is not found";
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));   
        this.setSelectedSupplier(supplierBeanLocal.findSupplierByName(this.getSelectedSupplier().getSupplierName()));
    }
        
    }
